package com.bst.common.base.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.bst.common.annotation.DataSource;
import com.bst.common.base.mapper.BaseMysqlMapper;
import com.bst.common.base.mapper.BaseSqlserverMapper;
import com.bst.common.base.service.IBaseDWService;
import com.bst.common.base.mapper.BaseMapper;
import com.bst.common.constant.JobConstant;
import com.bst.common.utils.StringUtils;
import com.bst.common.utils.sql.EtlUtil;
import com.bst.common.vo.DimVO;
import com.bst.common.vo.DwQueryParamVO;
import com.bst.common.vo.MdDimDataGenerateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunlei on 2021/6/1.
 */
@Service
public class BaseDWServiceImpl implements IBaseDWService {

    @Autowired
    private BaseMapper baseMapper;
    @Autowired
    private BaseSqlserverMapper baseSqlserverMapper;
    @Autowired
    private BaseMysqlMapper baseMysqlMapper;

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public List<Map<String, Object>> findBySql(String sql) {
        return baseMapper.findBySql(sql);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public List<Map<String, Object>> findPageBySql(String sql, Integer pageNum, Integer pageSize, String orderBy, String dbType) {
        sql = EtlUtil.getMaxNumSql(sql, pageSize, dbType, orderBy);
        return findBySql(sql);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public int insertMapList(String tableName, Map<String, Object> colMap, List<Map<String, Object>> mapList) {
        return baseMapper.insertMapList(tableName, colMap, mapList);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public int insertMap(String tableName, Map<String, Object> dataMap) {
        return baseMapper.insertMap(tableName, dataMap);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public int insert(String sql) {
        return baseMapper.insert(sql);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public List<Map<String, Object>> selectDataByTableName(String tableName, String flds) {
        return baseMapper.selectDataByTableName(tableName, flds);
    }

    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public List<Map<String, Object>> selectBaseDwData(DwQueryParamVO queryVO){
        String dbType = queryVO.getDbType();
        if("sqlserver".equals(dbType)) {
            return baseSqlserverMapper.selectCommonDwData(queryVO);
        } else {
            return baseMysqlMapper.selectCommonDwData(queryVO);
        }
    }


    @Override
    @DataSource(value = JobConstant.DB_CLS_DW)
    public List<Map<String,Object>> selectCommonDimData(MdDimDataGenerateVO vo){
        String dbType = vo.getDbType();
        if("sqlserver".equals(dbType)) {
            return null;
        } else {
            return baseMysqlMapper.selectCommonDimData(vo);
        }
    }

    @Override
    @DataSource(value= JobConstant.DB_CLS_DW)
    public List<Map<String, Object>> selectCommonDwData(DwQueryParamVO queryVO) {
        String dbType = queryVO.getDbType();
        if(StringUtils.isEmpty(queryVO.getStartDay()) && StringUtils.isEmpty(queryVO.getEndDay())) {
            return null;
        }
        String startDay = queryVO.getStartDay();
        String endDay = queryVO.getEndDay();
        if(queryVO.getDimVOList()==null) {
            queryVO.setDimVOList(new ArrayList<>());
        }
        String dateFld = queryVO.getDateFld().trim();
        String dayWhereSql="",dayJobinSql="";
        if("M".equals(queryVO.getDateType())) {
            queryVO.getDimVOList().add(new DimVO("dd.mm",null,dateFld));
            dayJobinSql = ("inner join md_dim_day dd on dd.id_day=dw."+dateFld);
            queryVO.setGroupBy(queryVO.getGroupBy()==null?"dd.mm":queryVO.getGroupBy()+",dd.mm");
            if(startDay.substring(0,7).equals(endDay.substring(0,7))) {
                dayWhereSql = ("dd.mm='"+startDay.substring(0,7)+"'");
            } else {
                dayWhereSql = ("dd.mm>='"+startDay.substring(0,7)+"' and dd.mm<'"+endDay.substring(0,7)+"'");
            }
            queryVO.setStartDay(null);queryVO.setEndDay(null);
        }  else if("Q".equals(queryVO.getDateType())) {
            queryVO.getDimVOList().add(new DimVO("dd.qq",null,dateFld));
            dayJobinSql = ("inner join md_dim_day dd on dd.id_day=dw."+dateFld);
            queryVO.setGroupBy(queryVO.getGroupBy()==null?"dd.qq":queryVO.getGroupBy()+",day.qq");
            if(startDay.substring(0,5).equals(endDay.substring(0,5))) {
                dayWhereSql = ("dd.qq='"+startDay.substring(0,5)+"'");
            } else {
                dayWhereSql = ("dd.qq>='"+startDay.substring(0,5)+"' and dd.qq<'"+endDay.substring(0,5)+"'");
            }
            queryVO.setStartDay(null);queryVO.setEndDay(null);
        }  else if("Y".equals(queryVO.getDateType())) {
            queryVO.getDimVOList().add(new DimVO("dd.yy",null,dateFld));
            dayJobinSql = ("inner join md_dim_day dd on dd.id_day=dw."+dateFld);
            queryVO.setGroupBy(queryVO.getGroupBy()==null?"dd.yy":queryVO.getGroupBy()+",day.yy");
            if(startDay.substring(0,4).equals(endDay.substring(0,4))) {
                dayWhereSql = ("dd.yy='"+startDay.substring(0,4)+"'");
            } else {
                dayWhereSql = ("dd.yy>='"+startDay.substring(0,4)+"' and dd.yy<'"+endDay.substring(0,4)+"'");
            }
            queryVO.setStartDay(null);queryVO.setEndDay(null);
        }
        if(StringUtils.isNotEmpty(queryVO.getWhereSql())) {
            queryVO.setWhereSql(queryVO.getWhereSql()+(StringUtils.isEmpty(dayWhereSql)?"":" and "+dayWhereSql));
        } else {
            queryVO.setWhereSql(dayWhereSql);
        }
        if(StringUtils.isNotEmpty(dayJobinSql)) {
            queryVO.setJoinTables(StringUtils.isEmpty(queryVO.getJoinTables())?dayJobinSql:dayJobinSql+" "+queryVO.getJoinTables());
        }
        if("sqlserver".equals(dbType)) {
            return baseSqlserverMapper.selectCommonDwData(queryVO);
        } else {
            return baseMysqlMapper.selectCommonDwData(queryVO);
        }
    }
}
